<?
session_start();
function GetConnection()
{
	return new mysqli('wyvern.cs.newpaltz.edu', 'N02036842', 's741780', 'N02036842_db');
}

function GetCurrentUser()
{
	$userId=$_SESSION['UserId'];
	$conn = GetConnection();
	$result = $conn->query("SELECT * FROM Users WHERE id=$userId");
	if($conn->error != null) return null;
	$rs = $result->fetch_assoc();
	$conn->close();
	return $rs;
}

function Login($email, $password)
{
	$conn = GetConnection();
	$result = $conn->query("SELECT U.id, U.Password FROM Users U JOIN ContactMethods CM ON U.id=CM.UserId WHERE CM.Value='$email'");
	if($conn->error != null) return array(Email => 'Invalid Email');
	$rs = $result->fetch_assoc();
	if(!$rs)return array(Email => 'No Such User');
	$conn->close();
	if($rs['Password'] != $password) return array(Password => 'Incorect Password');
	
	$_SESSION['UserId'] = $rs['id'];
	return null;
}